import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import validation_curve
from sklearn.datasets import load_iris
from sklearn.neighbors import KNeighborsClassifier#利用邻近点方式训练数据

# 加载数据
iris = load_iris()

# 定义模型
df_iris = pd.DataFrame(iris.data, columns=iris.feature_names)
df_iris['target'] = iris.target

dataX = df_iris.drop(columns=["target"]).values
dataY = df_iris['target'].values

knn=KNeighborsClassifier(n_neighbors=1)#引入训练方法

# 定义超参数范围
param_range = range(1, 10, 1)  # 比如 n_estimators 的范围
param_name = 'n_neighbors'

# 计算验证曲线
train_scores, test_scores = validation_curve(
    knn, dataX, dataY, param_name=param_name, param_range=param_range,
    cv=5, n_jobs=-1, scoring='accuracy'
)

# 计算平均分数和标准差
train_mean = np.mean(train_scores, axis=1)
train_std = np.std(train_scores, axis=1)
test_mean = np.mean(test_scores, axis=1)
test_std = np.std(test_scores, axis=1)

# 绘制验证曲线
plt.figure(figsize=(10, 6))
plt.plot(param_range, train_mean, label='Training score', color='blue', marker='o')
plt.fill_between(param_range, train_mean - train_std, train_mean + train_std, alpha=0.15, color='blue')
plt.plot(param_range, test_mean, label='Cross-validation score', color='green', marker='s')
plt.fill_between(param_range, test_mean - test_std, test_mean + test_std, alpha=0.15, color='green')

plt.title('Validation Curve')
plt.xlabel('Parameter: ' + param_name)
plt.ylabel('Score')
plt.legend(loc='best')
plt.grid()
plt.show()
